$(function () {
    getUrl("retrievePassword/verifyEmail");
    $("#new").val("");
    $("#agin").val("");
    //实时监控输入框
    inputChange("#new","#newPsdError1");
    inputChange("#agin","#newPsdError2");
    inputChange("#new","#deferentError");
    inputChange("#agin","#deferentError");
    ajaxGetUserForResetPwd();
    function newPsdMessage1(){
        var pass = $("#new").val();
        if(pass==null||pass==""){
            validateSpanMsg("newPsdError1","请输入新密码");
            return false;
        }else if(!(isPassword(pass))){
            validateSpanMsg("newPsdError1","密码格式不正确");
            return false;
        }else{
            return true;
        }
    }
    function newPsdMessage2(){
        var pass = $("#new").val();
        var agin = $("#agin").val();
        if (agin == null || agin == "") {
            validateSpanMsg("newPsdError2","请确认新密码");
            return false;
        }else if(agin != pass){
            validateSpanMsg("deferentError","两次输入的密码不一致");
            return false;
        }else{
            return true;
        }
    }
    $("#new").blur(function(){
        newPsdMessage1();
    })
    $("#agin").blur(function(){
        newPsdMessage2();
    })

    $("#accomplish").click(function () {
        var pass = $("#new").val();
        var agin = $("#agin").val();
        //
        //if(pass==""||pass==null){
        //    validateSpanMsg("newPsdError1","请输入新密码");
        //}else if(!isPassword(pass)) {
        //    validateSpanMsg("newPsdError1", "密码格式有误");
        //}else if(agin==""||agin==null){
        //    validateSpanMsg("newPsdError2","请确认新密码");
        //}else if (isPassword(pass)) {
        //    if (pass == agin) {
        //        ajaxResetPassword(pass);
        //    } else {
        //        validateSpanMsg("deferentError","两次密码输入不一致");
        //    }
        //}
        if(newPsdMessage1()&&newPsdMessage2()){
            ajaxResetPassword(pass);
        }
    });
});


/**
 * 获取用户绑定邮箱
 */
function ajaxGetUserForResetPwd() {
    var url = "/Lvscore_User_M/ajaxGetUserForResetPwd";
    $.post(
        url,
        function (data) {
            var text = data.name + "（" + data.email.replace(/(.{2}).+(.{2}@.+)/g, '$1****$2') + "）";
            $("#user_account").text(text);
        },
        "json"
    );
}

/**
 * 修改用户密码
 * @param password    用户输入的新密码
 */
function ajaxResetPassword(password) {
    var url = "/Lvscore_User_M/ajaxResetPassword";
    $.post(url, {'password': password}, function (data) {
        if (data == 1) {
            var cookie=new sweetCookie();
            cookie.setCookie("remember","",-1);
            window.location.href = "/Lvscore_User_M/userWeb/views/retrievePassword/findSucceedEmail.html"
        } else {
            validateSpanMsg("deferentError","服务器异常，请稍后重试！");
            //alertBox("服务器异常，请稍后重试！","")
        }
    });
}